package com.gaogzhen.designPattern.guardedSuspension;

import java.util.Hashtable;

/**
 * 信箱类
 */
public class MailBoxes {
    // 容器，存放信件
    private static Hashtable<String, GuardedDecouple> container = new Hashtable<>();

    /**
     * 根据居民房间编号初始化信箱
     * @param identifiers   居民房间编号
     */
    public static void init(String[] identifiers) {
        for (String identifier: identifiers) {
            container.put(identifier, new GuardedDecouple(identifier));
        }
    }

    /**
     * 根据房间编号获取信件
     * @param identifier    房间编号
     * @return              信件
     */
    public synchronized  static GuardedDecouple getLetter(String identifier) {
        return container.get(identifier);
    }

    /**
     * 设置信件-快递员把信件投入收件箱
     * @param guardedDecouple
     */
    public synchronized static void setLetter(GuardedDecouple guardedDecouple) {
        container.put(guardedDecouple.getIdentifier(), guardedDecouple);
    }
}
